package cn.jy.careuplusdoctor.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class AlertCommon {
	Activity	activity;

	public AlertCommon(Activity activity) {
		this.setActivity(activity);
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public AlertDialog updateDialog(final String apkUrl, final String savePath, final String fileName, String message) {
		AlertDialog.Builder builder = new Builder(this.getActivity());
		builder.setTitle("获取到新的应用.");
		if (!StringUtil.isNotEmpty(message)) {
			message = "有应用可以下载.";
		}
		builder.setMessage(message);
		builder.setPositiveButton("马上下载", new OnBtnClickListener(this.getActivity()) {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				CheckNewVersion.doUpdate(this.getActivity(), apkUrl, savePath, fileName);
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("忽略", new OnBtnClickListener(activity) {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.setCanceledOnTouchOutside(false);
		alert.show();
		return alert;
	}

	/**
	 * 弹出提示
	 * 
	 * @param title 标题
	 * @param msg 信息
	 * @param positiveText 肯定按钮文字
	 * @param positiveCallback 肯定回调
	 * @param negativeText 否定按钮文字
	 * @param negativeCallback 否定回调
	 * @return
	 */
	public AlertDialog alert(String title, String message, String positiveText, final Callback positiveCallback,
			String negativeText, final Callback negativeCallback) {
		AlertDialog.Builder builder = new Builder(this.getActivity());
		builder.setTitle(title);
		builder.setMessage(message);
		if (positiveCallback == null && negativeCallback == null) {
			builder.setPositiveButton("确认", new OnBtnClickListener(this.getActivity()) {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
		}
		if (positiveCallback != null) {
			builder.setPositiveButton(StringUtil.isNotEmpty(positiveText) ? positiveText : "确认",
					new OnBtnClickListener(this.getActivity()) {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							positiveCallback.callback(getActivity(), dialog, which);
						}
					});
		}
		if (negativeCallback != null) {
			builder.setNegativeButton(StringUtil.isNotEmpty(negativeText) ? negativeText : "取消",
					new OnBtnClickListener(activity) {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							negativeCallback.callback(getActivity(), dialog, which);
						}
					});
		}
		AlertDialog alert = builder.create();
		alert.show();
		return alert;
	}

	/**
	 * 弹出提示
	 * 
	 * @param title 标题
	 * @param msg 信息
	 * @param positiveText 肯定按钮文字
	 * @param positiveCallback 肯定回调
	 * @param negativeText 否定按钮文字
	 * @param negativeCallback 否定回调
	 * @return
	 */
	public AlertDialog alert(String title, String message, String positiveText, final Callback positiveCallback,
			String neutralText, final Callback neutralCallback, String negativeText, final Callback negativeCallback) {
		AlertDialog.Builder builder = new Builder(this.getActivity());
		builder.setTitle(title);
		builder.setMessage(message);
		if (positiveCallback == null && negativeCallback == null) {
			builder.setPositiveButton("确认", new OnBtnClickListener(this.getActivity()) {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
		}
		if (positiveCallback != null) {
			builder.setPositiveButton(StringUtil.isNotEmpty(positiveText) ? positiveText : "确认",
					new OnBtnClickListener(this.getActivity()) {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							positiveCallback.callback(getActivity(), dialog, which);
						}
					});
		}
		if (neutralCallback != null) {
			builder.setNeutralButton(StringUtil.isNotEmpty(neutralText) ? neutralText : "不确定",
					new OnBtnClickListener(this.getActivity()) {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							neutralCallback.callback(getActivity(), dialog, which);
						}
					});
		}
		if (negativeCallback != null) {
			builder.setNegativeButton(StringUtil.isNotEmpty(negativeText) ? negativeText : "取消",
					new OnBtnClickListener(activity) {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							negativeCallback.callback(getActivity(), dialog, which);
						}
					});
		}
		AlertDialog alert = builder.create();
		alert.show();
		return alert;
	}

	public AlertDialog alert(String title, String message) {
		return alert(title, message, null, null, null, null);
	}

	public AlertDialog alert(String title, String message, String positiveText, Callback positiveCallback) {
		return alert(title, message, positiveText, positiveCallback, null, null);
	}

	public abstract class OnBtnClickListener implements OnClickListener {
		private Activity	activity;

		public OnBtnClickListener(Activity activity) {
			this.setActivity(activity);
		}

		public Activity getActivity() {
			return activity;
		}

		public void setActivity(Activity activity) {
			this.activity = activity;
		}

		@Override
		public abstract void onClick(DialogInterface dialog, int which);
	}

	public interface Callback {
		public abstract void callback(Context context, DialogInterface dialog, int which);
	}
}
